package com.jlkf.controller;



import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.pojo.TblUser;
import com.jlkf.service.UserService;
import com.jlkf.view.ExcelUserInfo;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.ExportExcel;
import com.platform.server.util.StringTools;


@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	
	/**
	 * 用户列表
	 * @param startTime
	 * @param endTime
	 * @param uName
	 * @param phone
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("getUserInfo")
	@ResponseBody
	public ReturnData getUserInfo(String startTime,String endTime,String uName,String phone,Integer pageNumber,Integer pageSize){
		try{
			Map<String, Object> map = new HashMap<String,Object>();
			if(!StringTools.isEmpty(startTime)){
				map.put("startTime", startTime);
			}
			if(!StringTools.isEmpty(endTime)){
				map.put("endTime", endTime);
			}
			if(!StringTools.isEmpty(uName)){
				map.put("uName", uName);
			}
			if(!StringTools.isEmpty(phone)){
				map.put("phone", phone);
			}
			
			PageHelper.startPage(pageNumber,pageSize);
			List<TblUser> list = userService.getUserInfo(map);
			for(TblUser user:list){
			    SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			    String currentTime = formatDate.format(new Date());
			    Date today = formatDate.parse(currentTime);
			    if(!StringTools.isEmpty(user.getgUserBirthday())){
			    	Date birthDay = formatDate.parse(user.getgUserBirthday());
			    	user.setAge(today.getYear()-birthDay.getYear());
			    }
			}
			PageInfo<TblUser> pageInfo = new PageInfo<>(list);
			return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"用户列表");
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}
	
	/**
	 * 解封账号
	 */
	@RequestMapping("updateStatus")
	@ResponseBody
	public ReturnData updateStatus(Long gId,Integer type){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("gId", gId);
		map.put("type", type);
		int rel = userService.updateStatus(map);
		if(rel>0){
			return new ReturnData(200,"操作成功");
		}else{
			return new ReturnData(500,"操作失败");
		}
	}
	
	
	// 导出报表
	@RequestMapping("exportExcel")
	public void exportExcelInInvoice(HttpServletResponse response,String startTime,String endTime,String uName,String phone){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, Object> map = new HashMap<String,Object>();
		if(!StringTools.isEmpty(startTime)){
			map.put("startTime", startTime);
		}
		if(!StringTools.isEmpty(endTime)){
			map.put("endTime", endTime);
		}
		if(!StringTools.isEmpty(uName)){
			map.put("uName", uName);
		}
		if(!StringTools.isEmpty(phone)){
			map.put("phone", phone);
		}
		List<Object> list2 = new ArrayList<Object>();
		List<TblUser> list = userService.getUserInfo(map);
		for(TblUser user:list){
			ExcelUserInfo userInfo = new ExcelUserInfo();
			userInfo.setgId(user.getgId());
			if(user.getgLoginTime()!=null){
				userInfo.setgLoginTime(sf.format(user.getgLoginTime()));
			}
			userInfo.setgUserAddress(user.getgUserAddress());
			userInfo.setgUserBalance(user.getgUserMoney());
			userInfo.setgUserBirthday(user.getgUserBirthday());
			userInfo.setgUserCreateTime(sf.format(user.getgUserCreateTime()));
			userInfo.setgUserEducation(user.getgUserEducation());
			userInfo.setgUserGrow(user.getgUserGrow());
			userInfo.setgUserIntegral(user.getgUserIntegral().intValue());
			userInfo.setgUserName(user.getgUserName());
			userInfo.setgUserPhone(user.getgUserPhone());
			if(user.getgUserSex().equals("1")){
				userInfo.setSex("男");
			}else if(user.getgUserSex().equals("2")){
				userInfo.setSex("女");
			}else if(user.getgUserSex().equals("3")){
				userInfo.setSex("保密");
			}
			list2.add(userInfo);
		}
		String[] titles = { "昵称", "手机号", "ID", "性别", "出生日期", "学历", "积分", "成长值", "余额", "注册时间","最近登录时间","所在地"};
		ExportExcel.exportExcel("用户信息表.xls", titles, list2, response);
	}
	
	/**
	 * 查看详情
	 */
	@RequestMapping("getUserDetail")
	@ResponseBody
	public ModelAndView getUserDetail(Long gId){
		TblUser user = userService.getDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("user",user);
    	andView.setViewName("jsp/userManager/details");
		return andView;
	}
	
	/**
	 * 会员列表
	 * @param startTime
	 * @param endTime
	 * @param uName
	 * @param phone
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("getVipUserInfo")
	@ResponseBody
	public ReturnData getVipUserInfo(String startTime,String endTime,String uName,String phone,Integer pageNumber,Integer pageSize){
		try{
			Map<String, Object> map = new HashMap<String,Object>();
			if(!StringTools.isEmpty(startTime)){
				map.put("startTime", startTime);
			}
			if(!StringTools.isEmpty(endTime)){
				map.put("endTime", endTime);
			}
			if(!StringTools.isEmpty(uName)){
				map.put("uName", uName);
			}
			if(!StringTools.isEmpty(phone)){
				map.put("phone", phone);
			}
			
			PageHelper.startPage(pageNumber,pageSize);
			List<TblUser> list = userService.getUserInfo(map);
			for(TblUser user:list){
			    SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			    String currentTime = formatDate.format(new Date());
			    Date today = formatDate.parse(currentTime);
			    if(!StringTools.isEmpty(user.getgUserBirthday())){
			    	Date birthDay = formatDate.parse(user.getgUserBirthday());
			    	user.setAge(today.getYear()-birthDay.getYear());
			    }
			}
			PageInfo<TblUser> pageInfo = new PageInfo<>(list);
			return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"用户列表");
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}
}
